package com.dingwen.treasure.business.controller;

import com.dingwen.treasure.base.controller.BaseViewController;
import com.dingwen.treasure.base.enums.TaskType;
import com.dingwen.treasure.base.util.EnumUtil;
import com.dingwen.treasure.base.pojo.vo.Result;
import com.dingwen.treasure.business.component.bar.TestProgressBarTask;
import com.dingwen.treasure.core.vo.TaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 进度条任务API
 *
 * @author dingwen
 * @date 2022/12/07
 */
@Api(tags = "进度条任务API")
@RestController
@RequestMapping("bar")
public class ProgressBarTaskController implements BaseViewController {

    @Resource(name = "testProgressBarTask")
    private TestProgressBarTask testProgressBarTask;


    @ApiOperation(value = "提交进度条任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id"),
            @ApiImplicitParam(name = "taskType", value = "任务类型")
    })
    @PutMapping()
    public Result<TaskVo> submit(@RequestParam("taskId") String taskId, @RequestParam("taskType") String taskType) {
        TaskType taskTypeByCode = EnumUtil.getEnumByCode(TaskType.class, taskType);
        return success(
                testProgressBarTask.execute(
                        taskId,
                        taskTypeByCode,
                        100,
                        100
                )
        );
    }

    @ApiOperation(value = "查询任务进度")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id"),
            @ApiImplicitParam(name = "taskType", value = "任务类型")
    })
    @GetMapping("/{taskId}")
    public Result<TaskVo> queryProgress(@PathVariable("taskId") String taskId,
                                        @RequestParam("taskType") String taskType) {
        TaskType taskTypeByCode = EnumUtil.getEnumByCode(TaskType.class, taskType);
        return success(testProgressBarTask.queryProcess(taskId, taskTypeByCode));
    }

}
